﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace 流量控制试验台
{
    public class txt
    {
        public static void AddTextLog(string strLog)
        {
            DateTime dt = DateTime.Now;
            string file = System.Windows.Forms.Application.StartupPath + "\\TextLog\\" + String.Format("{0:D4}", dt.Year) + "-" + String.Format("{0:D2}", dt.Month) + "-" + String.Format("{0:D2}", dt.Day) + "运行信息.txt";
            FileInfo f = new FileInfo(file);
            // 如果文件所在的文件夹不存在则创建文件夹
            if (!Directory.Exists(f.DirectoryName))
                Directory.CreateDirectory(f.DirectoryName);
            FileStream fs;
            try
            {
                fs = new FileStream(file, FileMode.Open);
            }
            catch (FileNotFoundException)
            {
                fs = new FileStream(file, FileMode.Create);
            }
            catch
            {
                return;
            }
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            fs.Seek(0, SeekOrigin.End);
            sw.WriteLine(strLog);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();/**/
        }
    }
}
